<template>
  <el-container class="home-container">
    <!-- 顶部导航 -->
    <el-header class="header">
      <img class="logo" src="/logo.png" alt="系统logo" />
      <span class="system-name">物流管理系统</span>
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{ username }} ▼
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="personal">个人中心</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-header>

    <!-- 主体内容 -->
    <el-container>
      <!-- 左侧菜单 -->
      <el-aside width="200px" class="menu">
        <el-menu 
          router 
          class="el-menu-vertical-demo"
        >
          <!-- 采购管理（一级菜单） -->
          <el-menu-item index="/purchaseManage">
            <template #title>
              <el-icon><Document /></el-icon>
              <span>采购管理</span> 
            </template>
          </el-menu-item>

          <!-- 商品管理（可折叠一级菜单） -->
          <el-sub-menu index="/productManage" default-active="/productManage/stock" >
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>商品管理</span>
            </template>
            <!-- 商品库存列表（子菜单） -->
            <el-menu-item index="/productManage/stock">
              <template #title>
                <el-icon><Box /></el-icon> <!-- 盒子图标，代表库存存储 -->
                <span>商品库存列表</span>
              </template>
            </el-menu-item>
            <!-- 入库订单列表（子菜单） -->
            <el-menu-item index="/productManage/in">
              <template #title>
                <el-icon><ArrowDown /></el-icon> <!-- 向下箭头表示入库 -->
                <span>入库订单列表</span>
              </template>
            </el-menu-item>

            <!-- 出库订单列表（子菜单） -->
            <el-menu-item index="/productManage/out">
              <template #title>
                <el-icon><ArrowUp /></el-icon> <!-- 向上箭头表示出库 -->
                <span>出库订单列表</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>

      <!-- 右侧内容区 -->
      <el-main class="content">
        <!-- 面包屑 -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="index">
            {{ item.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
        
        <!-- 路由视图 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="js">
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
// 新增导入箭头图标（需确保已安装 @element-plus/icons-vue）
import { Document, Setting, Box, ArrowDown, ArrowUp } from '@element-plus/icons-vue'

const router = useRouter()
const username = ref('管理员')
const breadcrumbList = ref([])

// Update breadcrumb
const updateBreadcrumb = () => {
  breadcrumbList.value = router.currentRoute.value.matched
    .filter(item => item.meta?.title)
    .map(item => ({ title: item.meta.title }))
}

// Watch route changes
watch(
  () => router.currentRoute.value,
  () => updateBreadcrumb(),
  { immediate: true }
)

// Logout logic
const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('token')
    router.push('/login')
    ElMessage.success('退出登录成功')
  }
}
</script>

<style scoped>
.home-container {
  height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  padding: 0 20px;
  background: #2c3e50;
  color: white;
}

.logo {
  height: 40px;
  margin-right: 15px;
}

.system-name {
  font-size: 18px;
  margin-right: auto;
}

.el-dropdown-link {
  color: white;
  cursor: pointer;
}

.menu {
  background: #f8f9fa;
  border-right: 1px solid #e5e5e5;
}

/* 确保菜单标题可见 */
.el-submenu__title {
  color: #333;
  font-weight: 500;
}
</style>